package com.hqyj.javaSpringBoot.modules.test.dao;

import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.City;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CityDao {

    @Insert("insert into test_city(city_name,local_city_name,country_id,date_created) "+
    "values (#{cityName},#{localCityName},#{countryId},#{dateCreated})")
    @Options(useGeneratedKeys = true,keyColumn ="city_id",keyProperty = "cityId")
    void insertCity(City city);

    @Update("update test_city set city_name=#{cityName},local_city_name=#{localCityName} " +
            "where city_id=#{cityId}")
    void updateCity(City city);

    @Delete("delete from test_city where city_id=#{cityId}")
    void deleteCityById(int cityId);

    @Select("select * from test_city where city_id=#{cityId}")
    City getCityById(int cityId);

    @Select("select * from test_city where country_id=#{countryId}")
    List<City> getCitiesByCountryId(int countryId);

    /*@Select("<script>"
     +"select * from test_city" +
            "<where>" +
            "<if test='keyWord!=\"\" and keyWord!=null'>" +
            "and (city_name like '%${keyWord}%') or" +
            "local_city_name like '%${localCityName}%')" +
            "")*/
    @Select("<script>"
            + "select * from test_city "
            + "<where> "
            + "<if test='keyWord != \"\" and keyWord != null'>"
            + " and (city_name like '%${keyWord}%' or "
            + " local_city_name like '%${keyWord}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by city_id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<City> getCitiesBySearchBean(SearchBean searchBean);

//    @Insert("")
    @Insert("<script>"
        + "insert into test_city (city_name, local_city_name, country_id, district, "
        + "population, date_modified, date_created) values "
        + "<foreach collection='cities' item='city' index='index' separator=','>"
        + "(#{city.cityName}, #{city.localCityName}, #{city.countryId}, "
        + "#{city.district}, #{city.population}, #{city.dateModified}, #{city.dateCreated})"
        + "</foreach>"
        + "</script>")
    @Options(useGeneratedKeys = true,keyColumn = "city_id",keyProperty = "cityId")
    void insertCities( @Param("cities") List<City> cities);
}
